<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<!--引入外部样式-->
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link type="text/css" rel="stylesheet" href="./css/login.css"/>
</head>

<body>

<div style="text-align: center">
    <div class="head">
        <img src="images/logo_register.png" alt="" id="logo">
        <span style="color: #01b6eb; font-size: 35px; position: absolute; top:40px; left: 110px">_憩息阁_</span>
    </div>

    <div class="login">
        <div id="prompt_login">
            <span><img src="images/提示.png" alt="提示图片" id="prompt_logo"></span>
            <span id="prompt_mes">请输入用户名和密码</span><br>
        </div>
        <div id="form">
            <div>
                <span><img src="images/user.png" alt="" id="username_logo"></span>
                <input type="text" class="ipt" name="username" id="username" placeholder="请输入用户名 ">
            </div>

            <div>
                <span><img src="images/pwd.png" alt="" id="pwd_logo"></span>
                <input type="password" class="ipt" name="password" id="password" autocomplete="on"
                       placeholder="请输入密码 ">
            </div>
            <div class="remember">
                记住密码 <input type="checkbox" id="remember" name="remember" value="1"><br>
            </div>

            <input type="submit" id="submit" value=" 登             录 ">
            <div id="backToIndex">
                <a href="/HomePage.html">返回主页面</a>
            </div>
            <div id="goLogin">
                <span>未注册账号？</span><a href="/register.html">点击我去注册</a>
            </div>

        </div>
    </div>
</div>


<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="module">
    var prompt_mes = document.getElementById("prompt_mes");
    var username = document.getElementById("username");
    var password = document.getElementById("password");
    var remember = document.getElementById("remember");
    var cookies = document.cookie.split("; ");
    remember.addEventListener('click', function () {
        console.log(remember.checked)
    })
    if (cookies.length!=0) {
        for (var i = 0; i < cookies.length; i++) {
            var ck = cookies[i].split("=");
            if (ck[0] === "username") {
                username.value = ck[1];
            }
            if (ck[0] === "password") {
                password.value = ck[1];
                remember.checked = "checked";
            }
        }
    }



    //键盘事件
    document.addEventListener('keydown', function(event) {
        if (event.key === 'Enter') {
            var user = {
                userName: "",
                userPassWord: "",
                userDate: " "
            }

            user.userName = document.getElementById('username').value;
            user.userPassWord = document.getElementById('password').value;
            axios({
                url: "/checkLogin",
                method: 'post',
                data: {
                    "user": user,
                    "flag": remember.checked
                }
            }).then(resp => {
                if (resp.data.split(";")[0] == "success") {
                    var userDate = resp.data.split(";")[1];
                    user.userDate = userDate;
                    sessionStorage.setItem("user", JSON.stringify(user));
                    prompt_mes.innerText = "登录成功！";
                    setTimeout(function () {
                        location.href = "/HomePage.html";
                    }, 500)
                } else if (resp.data.split(";")[0] == "isAdmin") {
                    var userDate = resp.data.split(";")[1];
                    user.userDate = userDate;
                    sessionStorage.setItem("user", JSON.stringify(user));
                    sessionStorage.setItem("isAdmin", "isAdmin")
                    prompt_mes.innerText = "登录成功！";
                    setTimeout(function () {
                        location.href = "/Admin.html"
                    }, 500)

                } else {
                    // prompt_mes.innerText = "";
                    prompt_mes.innerText = resp.data;
                }
            })

        }
    })


    submit.addEventListener('click', function () {
        var user = {
            userName: "",
            userPassWord: "",
            userDate: " "
        }

        user.userName = document.getElementById('username').value;
        user.userPassWord = document.getElementById('password').value;
        axios({
            url: "/checkLogin",
            method: 'post',
            data: {
                "user": user,
                "flag": remember.checked
            }
        }).then(resp => {
            if (resp.data.split(";")[0] == "success") {
                var userDate = resp.data.split(";")[1];
                user.userDate = userDate;
                sessionStorage.setItem("user", JSON.stringify(user));
                prompt_mes.innerText = "登录成功！";
                setTimeout(function () {
                    location.href = "/HomePage.html";
                }, 500)
            } else if (resp.data.split(";")[0] == "isAdmin") {
                var userDate = resp.data.split(";")[1];
                user.userDate = userDate;
                sessionStorage.setItem("user", JSON.stringify(user));
                sessionStorage.setItem("isAdmin", "isAdmin")
                prompt_mes.innerText = "登录成功！";
                setTimeout(function () {
                    location.href = "/Admin.html"
                }, 500)

            } else {
                // prompt_mes.innerText = "";
                prompt_mes.innerText = resp.data;
            }
        })
    })
</script>
</body>
</html>